package com.scau.yangsuiyu.estore.controller.buyer;

import com.alibaba.fastjson.JSONObject;
import com.scau.yangsuiyu.estore.controller.MainStageController;
import com.scau.yangsuiyu.estore.controller.buyer.manageBasicInfo.ManageBuyerInfo;
import com.scau.yangsuiyu.estore.controller.myComponent.BuyerOrderHBox;
import com.scau.yangsuiyu.estore.controller.myComponent.TopSpan;
import com.scau.yangsuiyu.estore.dababases.dao.OrderDao;
import com.scau.yangsuiyu.estore.dababases.entity.Buyer;
import com.scau.yangsuiyu.estore.dababases.entity.Orders;
import com.scau.yangsuiyu.estore.service.AppService;
import de.felixroske.jfxsupport.FXMLController;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.control.Pagination;
import javafx.scene.control.Tab;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.VBox;

import java.io.IOException;
import java.net.URL;
import java.util.List;
import java.util.ResourceBundle;

/**
 * @author suiyu_yang
 * @description 买家界面
 * @date 2019-05-23 20:00
 * @email suiyu_yang@163.com
 */
//@FXMLController
public class BuyerLayoutController extends AnchorPane implements Initializable {
    private MainStageController mainStageController;
    private Buyer buyer;
    private TopSpan topSpan;
    private int itemsPerPage = 5;
    private VBox orderListVBox = new VBox();
    private List<Orders> ordersList;

    @FXML
    private VBox mainVBox;

    @FXML
    private AnchorPane topSpanContainer;

    @FXML
    private Tab buyerDetailTab;

    @FXML
    private Tab manageShopTab;

    @FXML
    private Tab manageOrdersTab;

    @FXML
    private Pagination pagination;


    public BuyerLayoutController(JSONObject buyerJson) {
        this.mainStageController = buyerJson.getObject("MainStageController", MainStageController.class);
        this.buyer = buyerJson.getObject("user", Buyer.class);
        this.topSpan = new TopSpan(buyer);
        this.ordersList = new OrderDao().getOrdersByBuyer(buyer);
        loadFXML();
    }


    private void loadFXML() {
        FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/view/buyer/BuyerLayout.fxml"));
        fxmlLoader.setController(this);
        fxmlLoader.setRoot(this);
        try {
            fxmlLoader.load();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        ManageBuyerInfo manageBuyerInfo = new ManageBuyerInfo(buyer);
        buyerDetailTab.setContent(manageBuyerInfo);

        topSpanContainer.getChildren().add(topSpan);

        updatePages();
        mainVBox.getChildren().add(orderListVBox);
    }

    @FXML
    private void gotoCommentPane() {
        new AppService().gotoCommentGoodsPane();
    }


    /**
     * 更新页面
     */
    private void updatePages() {
        orderListVBox.getChildren().clear();

        int pageNum;
        if (ordersList.size() % itemsPerPage == 0) {
            pageNum = ordersList.size() / itemsPerPage;
        } else {
            pageNum = ordersList.size() / itemsPerPage + 1;
        }
        pagination.setPageCount(pageNum);
        pagination.setPageFactory((Integer pageIndex) -> createPage(pageIndex));
    }

    /**
     * 创建每一页的对象
     *
     * @param pageIndex
     * @return
     */
    private VBox createPage(int pageIndex) {
        int page = pageIndex * itemsPerPage; //当前是第几个商品
        if (ordersList.size() != 0) {
            orderListVBox.getChildren().clear();
            for (int i = page; i < page + itemsPerPage; i++) {
                if (i < ordersList.size()) {
                    Orders temp = ordersList.get(i);
                    BuyerOrderHBox buyerOrderHBox = new BuyerOrderHBox(temp);
                    orderListVBox.getChildren().add(buyerOrderHBox);
                }
            }
        }
        return orderListVBox;
    }

}
